#include "trng_bsp.h"
#include "button_bsp.h"
#include "flash_bsp.h"
#include "main.h"

#include "dino_draw.h"
#include "dino_game.h"

#define DINO_GAME_FPS 35
#define DINO_GAME_TICKS (1000 / DINO_GAME_FPS)

#define GAME_SPEED_DEFAULT 4.0f
#define GAME_SPEED_MAX 7.0f
#define CLOUD_SPEED_LEVEL GAME_SPEED_DEFAULT / 5.0f

#define DINO_LEG_CHANGE_TIME_DEFAULT 12
#define DINO_LEG_CHANGE_TIME_MIN 2

#define DINO_RUN_SPEED_DEFAULT 4.8f
#define DINO_RUN_SPEED_TOP (0.0f)
#define DINO_RUN_SPEED_TOP2 (-3.0f)

#define DINO_JUMP_UP_ACCEL 0.4f
#define DINO_JUMP_FALL_ACCEL 0.4f

#define DINO_SCORE_INC_COUNT 1

typedef struct xGameParameters
{
    GameFsm_t DinoGame;

    uint32_t Score;
    uint32_t LastBestScore;

    float Ground_x;
    float Ground_y;
    float GroundSpeed;

    float Cloud_x[2];
    float Cloud_y[2];
    float CloudSpeed;

    uint16_t Cactus_index;
    int16_t Cactus_width;
    float Cactus_x;
    float Cactus_y;
    float CactusSpeed;

    float Dino_x;
    float Dino_y;
    uint16_t DinoLeg;
    uint16_t DinoRunSpeed;
    uint16_t DinoRunCnt;
    uint16_t JumpDir; /* 0  上升  1 下降 */
    float Jump_y;
    float JumpSpeed;

    uint16_t LowBatteryShowCount;
    uint16_t ThemeType;
    uint16_t EasterEggTickCount;
    int16_t Dino_girl_x;
    int8_t MyLove_y;
    uint8_t Heart_index;
    int8_t Heart_y;
    uint8_t EasterEggState;

} GameParameters_t;

GameParameters_t GamePar;

static void dinoGameFsmSwitch(uint32_t delta_tick)
{
    uint8_t button = buttonGetStatus();
    static uint8_t score_set_flag = 0;
    static uint8_t theme_set_flag = 0;
    static uint8_t easter_egg_flag = 0;
    switch (GamePar.DinoGame)
    {
    case GameHello:
        if (easter_egg_flag == 1 && button != BUTTON_NOPRESS)
            break;
        else if (easter_egg_flag == 1)
            easter_egg_flag = 0;
        if (button == BUTTON_CLICK && theme_set_flag == 0)
            GamePar.DinoGame = GameStart;
        else if (button == BUTTON_SOLONGPRESS)
        {
            GamePar.LastBestScore = 0;
            score_set_flag = 1;
        }
        else if (button == BUTTON_LONGLONGPRESS && theme_set_flag == 0)
        {
            GamePar.ThemeType = GamePar.ThemeType == 0 ? 1 : 0;
            displayInversionSet(GamePar.ThemeType);
            theme_set_flag = 1;
        }
        else if (button == BUTTON_NOPRESS && (theme_set_flag == 1 || score_set_flag == 1))
        {
            theme_set_flag = 0;
            uint32_t data[2];
            data[0] = GamePar.LastBestScore;
            data[1] = GamePar.ThemeType;
            saveParam(data, sizeof(data));
        }
        break;
    case GameReady:
        if (button == BUTTON_CLICK)
            GamePar.DinoGame = GameRun;
        break;
    case GameRun:
        if (button == BUTTON_PRESS)
            GamePar.DinoGame = GameJumpUp;
        else if (button >= BUTTON_LONGPRESS)
            GamePar.DinoGame = GameJumpUpHigh;
        break;
    case GameJumpUp:
        if (button == BUTTON_LONGPRESS)
            GamePar.DinoGame = GameJumpUpHigh;
    case GameJumpUpHigh:
        break;
    case GameOver:
        if (button == BUTTON_CLICK)
            GamePar.DinoGame = GameStart;
        break;
    case GameEasterEgg:
        easter_egg_flag = 1;
        if (button == BUTTON_CLICK && GamePar.EasterEggState == 10)
            GamePar.EasterEggState = 11;
        else if (button == BUTTON_LONGLONGPRESS)
            GamePar.DinoGame = GameHello;
        break;
    case GameLowBattery:
        break;
    default:
        break;
    }
}

static void dinoGmaeEasterEggShow(uint32_t delta_tick)
{
    uint32_t random;
    GamePar.EasterEggTickCount++;
    if (GamePar.EasterEggState == 0) /* 初始化状态变量 */
    {
        GamePar.GroundSpeed = 2;
        GamePar.Dino_x = 10;
        GamePar.Dino_y = 42;
        GamePar.JumpSpeed = DINO_RUN_SPEED_DEFAULT;
        GamePar.JumpDir = 0;
        /* 创造一个植物 */
        random = randomNumGet();
        GamePar.Cactus_index = random % getCactusCounts();
        GamePar.Cactus_x = random % 120 + (float)DISPLAY_WIDTH;
        GamePar.Cactus_y = DISPLAY_HEIGHT - 8 - getCactusHeight(GamePar.Cactus_index);
        GamePar.Cactus_width = getCactusWidth(GamePar.Cactus_index);
        GamePar.CactusSpeed = GamePar.GroundSpeed;

        GamePar.Dino_girl_x = 300;
        GamePar.MyLove_y = 15;
        GamePar.Heart_y = 35;
        GamePar.EasterEggState++;

        uint32_t data[2];
        data[0] = GamePar.Score;
        data[1] = GamePar.ThemeType;
        saveParam(data, sizeof(data));
    }
    else if (GamePar.EasterEggState == 1) /* 画面1 显示 congratulations you win */
    {
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        for (uint8_t i = 0; i < 2; i++)
            drawCloud(GamePar.Cloud_x[i], GamePar.Cloud_y[i]);
        drawCactus(GamePar.Cactus_x, GamePar.Cactus_y, GamePar.Cactus_index);
        drawDino(GamePar.Dino_x, GamePar.Jump_y, DINO_FLYING);
        drawNum(90, 2, GamePar.Score, COLOR_WHITE);

        if (GamePar.EasterEggTickCount * delta_tick > 3000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 2) /* 画面1 显示 congratulations you win */
    {
        drawCongratulations(15, 18);
        drawYOUWIN(40, 40);
        if (GamePar.EasterEggTickCount * delta_tick > 5000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 3) /* 画面2 小恐龙继续奔跑跨过一个植物 */
    {
        /* 绘制地面 */
        GamePar.Ground_x -= GamePar.GroundSpeed;
        if (GamePar.Ground_x < ((float)DISPLAY_WIDTH) - 719)
            GamePar.Ground_x = 0;
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制植物 */
        GamePar.Cactus_x -= GamePar.CactusSpeed;
        drawCactus(GamePar.Cactus_x, GamePar.Cactus_y, GamePar.Cactus_index);
        if (GamePar.Cactus_x > 35) /* 植物到达恐龙位置 */
        {
            /* 绘制奔跑小恐龙 */
            GamePar.DinoRunCnt++;
            if (GamePar.DinoRunCnt > DINO_LEG_CHANGE_TIME_MIN)
            {
                GamePar.DinoRunCnt = 0;
                GamePar.DinoLeg++;
                GamePar.DinoLeg %= 2;
            }
            GamePar.Jump_y = GamePar.Dino_y;
            drawDino(GamePar.Dino_x, GamePar.Dino_y, GamePar.DinoLeg);
        }
        else
        {
            /* 绘制跳跃小恐龙 */
            if (GamePar.JumpDir == 0) /* 上升 */
            {
                GamePar.Jump_y -= GamePar.JumpSpeed >= 0.0f ? GamePar.JumpSpeed : 0.0f;
                GamePar.JumpSpeed -= DINO_JUMP_UP_ACCEL;
                /* 处理长按跳高的情况 */
                if (GamePar.JumpSpeed <= DINO_RUN_SPEED_TOP)
                {
                    GamePar.JumpDir = 1;
                    GamePar.JumpSpeed = 0.0f;
                }
            }
            else /* 下降 */
            {
                GamePar.Jump_y += GamePar.JumpSpeed;
                GamePar.JumpSpeed += DINO_JUMP_FALL_ACCEL;
                if (GamePar.Jump_y >= GamePar.Dino_y)
                {
                    GamePar.JumpDir = 0;
                    GamePar.Jump_y = GamePar.Dino_y;
                    GamePar.JumpSpeed = DINO_RUN_SPEED_DEFAULT;
                    GamePar.EasterEggState++;
                }
            }
            drawDino(GamePar.Dino_x, GamePar.Jump_y, DINO_FLYING);
        }
    }
    else if (GamePar.EasterEggState == 4)
    {
        /* 绘制地面 */
        GamePar.Ground_x -= GamePar.GroundSpeed;
        if (GamePar.Ground_x < ((float)DISPLAY_WIDTH) - 719)
            GamePar.Ground_x = 0;
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制奔跑小恐龙 */
        GamePar.DinoRunCnt++;
        if (GamePar.DinoRunCnt > DINO_LEG_CHANGE_TIME_MIN)
        {
            GamePar.DinoRunCnt = 0;
            GamePar.DinoLeg++;
            GamePar.DinoLeg %= 2;
        }
        drawDino(GamePar.Dino_x, GamePar.Dino_y, GamePar.DinoLeg);
        /* 绘制girl 小恐龙 */
        GamePar.Dino_girl_x -= GamePar.GroundSpeed;
        drawDinoGirl(GamePar.Dino_girl_x, 41, DINO_GIRL_NORMAL);
        if (GamePar.Dino_girl_x <= 110)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 5)
    {
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制girl */
        drawDinoGirl(GamePar.Dino_girl_x, 41, DINO_GIRL_NORMAL);
        /* 绘制boy */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        if (GamePar.EasterEggTickCount * delta_tick > 2000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 6)
    {
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制girl */
        drawDinoGirl(GamePar.Dino_girl_x, 41, DINO_GIRL_NORMAL);
        /* 绘制boy */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        drawIFoundU(10, 15);
        if (GamePar.EasterEggTickCount * delta_tick > 3000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 7)
    {
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制girl */
        drawDinoGirl(GamePar.Dino_girl_x, 41, DINO_GIRL_NORMAL);
        /* 绘制boy */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        drawMyLove(43, GamePar.MyLove_y);
        if (GamePar.EasterEggTickCount * delta_tick > 2000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 8)
    {
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制girl */
        drawDinoGirl(GamePar.Dino_girl_x, 41, DINO_GIRL_NORMAL);
        /* 绘制boy */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        /* 绘制笑颜 */
        drawDinoSmail(GamePar.Dino_x, GamePar.Dino_y);
        drawDinoGirlSmail(GamePar.Dino_girl_x, 41);
        /* 绘制mylove */
        drawMyLove(43, GamePar.MyLove_y);
        /* 绘制闪动的心 */
        // GamePar.DinoRunCnt++;
        // if (GamePar.DinoRunCnt > 10)
        // {
        //     GamePar.DinoRunCnt = 0;
        //     GamePar.Heart_index++;
        //     GamePar.Heart_index %= 2;
        // }
        // drawHeart(GamePar.Heart_index, 60, GamePar.Heart_y);
        if (GamePar.EasterEggTickCount * delta_tick > 2000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 9 || GamePar.EasterEggState == 10)
    {
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        GamePar.DinoRunCnt++;
        if (GamePar.DinoRunCnt % 2 == 0)
        {
            GamePar.Dino_girl_x -= 1;
            GamePar.Dino_x += 1;
            GamePar.MyLove_y -= 1;
            GamePar.Heart_y -= 1;
            GamePar.Dino_girl_x = GamePar.Dino_girl_x > 72 ? GamePar.Dino_girl_x : 72;
            GamePar.Dino_x = GamePar.Dino_x < 48 ? GamePar.Dino_x : 48;
            GamePar.MyLove_y = GamePar.MyLove_y > 5 ? GamePar.MyLove_y : 5;
            GamePar.Heart_y = GamePar.Heart_y > 20 ? GamePar.Heart_y : 20;
        }
        if (GamePar.DinoRunCnt % 4 == 0)
        {
            GamePar.DinoLeg++;
            GamePar.DinoLeg %= 2;
        }
        if (GamePar.DinoRunCnt > 10)
        {
            GamePar.DinoRunCnt = 0;
            if (GamePar.Dino_x == 48 && GamePar.Dino_girl_x == 72)
            {
                GamePar.Heart_index++;
                GamePar.Heart_index %= 2;
            }
        }
        /* 绘制girl */
        drawDinoGirl(GamePar.Dino_girl_x, 41, GamePar.DinoLeg);
        /* 绘制boy */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, GamePar.DinoLeg);
        /* 绘制笑颜 */
        drawDinoSmail(GamePar.Dino_x, GamePar.Dino_y);
        drawDinoGirlSmail(GamePar.Dino_girl_x, 41);
        /* 绘制mylove */
        drawMyLove(43, GamePar.MyLove_y);
        /* 绘制闪动的心 */
        drawHeart(GamePar.Heart_index, 60, GamePar.Heart_y);
        if (GamePar.EasterEggTickCount * delta_tick > 8000)
        {
            GamePar.EasterEggTickCount = 0;
            GamePar.EasterEggState++;
        }
    }
    else if (GamePar.EasterEggState == 11)
    {
        /* 绘制二维码 */
        drawqrCode(36, 0);
    }
}

static void dinoGameFSM(uint32_t delta_tick)
{
    uint32_t random = 0;
    switch (GamePar.DinoGame)
    {
    case GameHello:                                          /* 欢迎界面，显示logo和历史最佳成绩 */
        GamePar.DinoRunSpeed = DINO_LEG_CHANGE_TIME_DEFAULT; /* 换腿的周期 */
        GamePar.DinoRunCnt++;
        if (GamePar.DinoRunCnt > GamePar.DinoRunSpeed)
        {
            GamePar.DinoRunCnt = 0;
            GamePar.DinoLeg++;
            GamePar.DinoLeg %= 2;
        }
        drawChromeDinoRun(GamePar.DinoLeg, 0, 0);
        drawHI(95, 18);
        drawNum(80, 40, GamePar.LastBestScore, COLOR_WHITE);
        break;
    case GameStart:
        /* 绘制地面 */
        random = randomNumGet();
        GamePar.Ground_x = 0 - (float)(random % (719 - DISPLAY_WIDTH));
        GamePar.Ground_y = DISPLAY_HEIGHT - 11;
        GamePar.GroundSpeed = GAME_SPEED_DEFAULT;
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制云朵 */
        random = randomNumGet();
        for (uint8_t i = 0; i < 2; i++)
        {
            GamePar.Cloud_x[i] = random % DISPLAY_WIDTH + i * 45;
            GamePar.Cloud_y[i] = random % 24;
            GamePar.CloudSpeed = CLOUD_SPEED_LEVEL;
            drawCloud(GamePar.Cloud_x[i], GamePar.Cloud_y[i]);
        }
        /* 没有植物，但选一个开始之后的第一个植物 */
        random = randomNumGet();
        GamePar.Cactus_index = random % getCactusCounts();
        GamePar.Cactus_x = random % 120 + (float)DISPLAY_WIDTH;
        GamePar.Cactus_y = DISPLAY_HEIGHT - 8 - getCactusHeight(GamePar.Cactus_index);
        GamePar.Cactus_width = getCactusWidth(GamePar.Cactus_index);
        GamePar.CactusSpeed = GAME_SPEED_DEFAULT;
        /* 绘制恐龙 */
        GamePar.Dino_x = 10; /* 恐龙站位 */
        GamePar.Dino_y = 42;
        GamePar.DinoRunSpeed = DINO_LEG_CHANGE_TIME_DEFAULT; /* 换腿的周期 */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        /* 绘制得分 */
        GamePar.Score = 0;
        drawNum(90, 2, GamePar.Score, COLOR_WHITE);
        GamePar.DinoGame = GameReady;
        break;
    case GameReady:
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制云朵 */
        for (uint8_t i = 0; i < 2; i++)
            drawCloud(GamePar.Cloud_x[i], GamePar.Cloud_y[i]);
        /* 没有植物，但选一个开始之后的第一个植物 */
        /* 绘制恐龙 */
        drawDino(GamePar.Dino_x, GamePar.Dino_y, DINO_FLYING);
        /* 绘制得分 */
        drawNum(90, 2, GamePar.Score, COLOR_WHITE);
        break;
    case GameRun:
    case GameJumpUp:
    case GameJumpUpHigh:
        /* 绘制地面 */
        GamePar.Ground_x -= GamePar.GroundSpeed;
        if (GamePar.Ground_x < ((float)DISPLAY_WIDTH) - 719)
            GamePar.Ground_x = 0;
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制云朵 */
        random = randomNumGet();
        for (uint8_t i = 0; i < 2; i++)
        {
            GamePar.Cloud_x[i] -= GamePar.CloudSpeed;
            if (GamePar.Cloud_x[i] < -45)
            {
                GamePar.Cloud_x[i] = random % 45 + (float)DISPLAY_WIDTH + i * 45;
                GamePar.Cloud_y[i] = random % 24;
            }
            drawCloud(GamePar.Cloud_x[i], GamePar.Cloud_y[i]);
        }
        /* 绘制植物 */
        GamePar.Cactus_x -= GamePar.CactusSpeed;
        if (GamePar.Cactus_x < -GamePar.Cactus_width)
        {
            random = randomNumGet();
            GamePar.Cactus_index = random % getCactusCounts();
            GamePar.Cactus_x = random % 120 + (float)DISPLAY_WIDTH;
            GamePar.Cactus_y = DISPLAY_HEIGHT - 8 - getCactusHeight(GamePar.Cactus_index);
            GamePar.Cactus_width = getCactusWidth(GamePar.Cactus_index);
        }
        drawCactus(GamePar.Cactus_x, GamePar.Cactus_y, GamePar.Cactus_index);
        /* 绘制恐龙 */
        if (GamePar.DinoGame == GameRun)
        {
            GamePar.Jump_y = GamePar.Dino_y;
            GamePar.JumpSpeed = DINO_RUN_SPEED_DEFAULT;
            GamePar.JumpDir = 0;
            GamePar.DinoRunCnt++;
            if (GamePar.DinoRunCnt > GamePar.DinoRunSpeed)
            {
                GamePar.DinoRunCnt = 0;
                GamePar.DinoLeg++;
                GamePar.DinoLeg %= 2;
            }
            drawDino(GamePar.Dino_x, GamePar.Dino_y, GamePar.DinoLeg);
        }
        else
        {
            if (GamePar.JumpDir == 0) /* 上升 */
            {
                GamePar.Jump_y -= GamePar.JumpSpeed >= 0.0f ? GamePar.JumpSpeed : 0.0f;
                GamePar.JumpSpeed -= DINO_JUMP_UP_ACCEL;
                /* 处理长按跳高的情况 */
                if ((GamePar.DinoGame == GameJumpUp && GamePar.JumpSpeed <= DINO_RUN_SPEED_TOP) || (GamePar.DinoGame == GameJumpUpHigh && GamePar.JumpSpeed <= DINO_RUN_SPEED_TOP2))
                {
                    GamePar.JumpDir = 1;
                    GamePar.JumpSpeed = 0.0f;
                }
            }
            else /* 下降 */
            {
                GamePar.Jump_y += GamePar.JumpSpeed;
                GamePar.JumpSpeed += DINO_JUMP_FALL_ACCEL;
                if (GamePar.Jump_y >= GamePar.Dino_y)
                {
                    GamePar.JumpDir = 0;
                    GamePar.Jump_y = GamePar.Dino_y;
                    GamePar.JumpSpeed = DINO_RUN_SPEED_DEFAULT;
                    GamePar.DinoGame = GameRun;
                }
            }
            drawDino(GamePar.Dino_x, GamePar.Jump_y, DINO_FLYING);
        }
        /* 处理碰撞 */
        if (GamePar.DinoGame == GameRun)
        {
            /* 当Dino位于地面时，在仙人掌身上则结束游戏 */
            int8_t x1 = GamePar.Dino_x + getDinoWidth(DINO_RUNNING1) - GamePar.Cactus_x;
            int8_t x2 = GamePar.Dino_x - GamePar.Cactus_x - GamePar.Cactus_width;
            if (x1 > 0 && x2 < 0)
                GamePar.DinoGame = GameOver;
        }
        else
        {
            /* 处理空中碰撞问题 */
            int8_t x1 = GamePar.Dino_x + getDinoWidth(DINO_FLYING) * 3 / 4 - GamePar.Cactus_x;
            int8_t x2 = GamePar.Dino_x + getDinoWidth(DINO_FLYING) / 4 - (GamePar.Cactus_x + GamePar.Cactus_width);
            int8_t y1 = GamePar.Jump_y + getDinoHeight(DINO_FLYING) / 2 - GamePar.Cactus_y;
            if (x1 > 0 && x2 < 0 && y1 > 0)
                GamePar.DinoGame = GameOver;
        }
        /* 绘制得分 */
        GamePar.Score += DINO_SCORE_INC_COUNT;
        drawNum(90, 2, GamePar.Score, COLOR_WHITE);
        /* 难度速度计算, 如果积分到了99999，进入彩蛋模式 */
        if (GamePar.Score > 1000)
        {
            if (GamePar.Score % 1000 == 0)
            {
                GamePar.GroundSpeed += 0.3f;
                if (GamePar.GroundSpeed > GAME_SPEED_MAX)
                    GamePar.GroundSpeed = GAME_SPEED_MAX;
                GamePar.CactusSpeed = GamePar.GroundSpeed;
                GamePar.CloudSpeed = GamePar.GroundSpeed / 5.0f;
            }
            if (GamePar.Score % 1000 == 0)
            {
                GamePar.DinoRunSpeed -= 1;
                if (GamePar.DinoRunSpeed < DINO_LEG_CHANGE_TIME_MIN)
                    GamePar.DinoRunSpeed = DINO_LEG_CHANGE_TIME_MIN;
            }
            /* 满分进入彩蛋 */
            if (GamePar.Score >= 99999)
            {
                GamePar.Score = 99999;
                GamePar.LastBestScore = GamePar.Score;
                GamePar.DinoGame = GameEasterEgg;
            }
        }
        break;
    case GameOver:
        if (GamePar.LastBestScore < GamePar.Score)
        {
            GamePar.LastBestScore = GamePar.Score;
            uint32_t data[2];
            data[0] = GamePar.LastBestScore;
            data[1] = GamePar.ThemeType;
            LOG_I("GameOver data[0]=%d, data[1]=%d", data[0], data[1]);
            saveParam(data, sizeof(data));
        }
        /* 绘制地面 */
        drawGround(GamePar.Ground_x, GamePar.Ground_y);
        /* 绘制云朵  */
        // drawCloud(GamePar.Cloud_x, GamePar.Cloud_y);
        /* 绘制植物 */
        drawCactus(GamePar.Cactus_x, GamePar.Cactus_y, GamePar.Cactus_index);
        /* 绘制恐龙 */
        drawDino(GamePar.Dino_x, GamePar.Jump_y, DINO_AFRAID);
        /* 绘制得分 */
        drawHI(0, 2);
        drawNum(22, 2, GamePar.LastBestScore, COLOR_WHITE);
        drawNum(90, 2, GamePar.Score, COLOR_WHITE);
        /* 绘制gameover */
        drawGameOver(8, 22);
        /* 绘制restart */
        drawRestartLogo(62, 40);
        break;
    case GameEasterEgg:
        /*播放彩蛋动画*/
        dinoGmaeEasterEggShow(delta_tick);
        break;
    case GameLowBattery:
        /* 电量低画面 */
        drawLowBattery(8, 28);
        if (delta_tick * GamePar.LowBatteryShowCount > 1000)
            GamePar.LowBatteryShowCount = 0;
        else if (delta_tick * GamePar.LowBatteryShowCount > 500)
            drawLowBattery_1(8, 28);
        GamePar.LowBatteryShowCount++;
        break;
    default:
        break;
    }
}

void dinoGameInit(uint8_t lowpower)
{
    if (lowpower == 1)
    {
        GamePar.DinoGame = GameLowBattery;
        return;
    }
    GamePar.DinoGame = GameHello;
    GamePar.Score = 0;
    uint32_t data[2];

    readParam(data, sizeof(data));
    LOG_I("dinoGameInit data[0]=%d, data[1]=%d", data[0], data[1]);
    if (data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF)
    {
        GamePar.LastBestScore = 0;
        GamePar.ThemeType = 0;
    }
    else
    {
        GamePar.LastBestScore = data[0];
        GamePar.ThemeType = data[1];
    }
    displayInversionSet(GamePar.ThemeType);
}

static void delayUntil(uint32_t *last, uint32_t ms)
{
    while (systick_get_ms() - *last < ms)
        ;
    *last = systick_get_ms();

    // static uint32_t time = 0;
    // LOG_I("delayUniil %d", *last - time);
    // time = *last;
}

void dinoGameProcess(void)
{
    static uint32_t lastTime = 0;
    static uint8_t first_flag = 0;
    if (first_flag == 0)
    {
        lastTime = systick_get_ms();
        first_flag = 1;
    }
    dinoGameFsmSwitch(DINO_GAME_TICKS);
    dinoGameFSM(DINO_GAME_TICKS);
    displayRefresh();

    delayUntil(&lastTime, DINO_GAME_TICKS);
}

uint8_t dinoGameGetState(void)
{
    return GamePar.DinoGame;
}

void dinoGameSetLowPowerShow(void)
{
    GamePar.DinoGame = GameHello;
    dinoGameProcess();
}
